<?php
session_start();  //启动会话
//如果未登录，没有设置session变量“stuNo”，跳转到登录页
if (!isset($_SESSION['stuNo'])) {
    header('Location:Login.php?frompage=course');
}
?>
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>课程管理</title>
    
</head>

<body>
    <?php
    include 'HeaderNav.html';  //包含页头与导航页
    ?>
    <h1>课程管理</h1>
    <?php
    //引用数据库连接文件
    require_once 'Conn.php';
    mysqli_query($db, 'set names utf8');  
    @$allNum = allNews();
       
    @$pageSize = 3; //约定没页显示几条信息
    
    @$pageNum = empty($_GET["pageNum"])?1:$_GET["pageNum"];
    
    @$endPage = ceil($allNum/$pageSize); //总页数
    
    @$array = news($pageNum,$pageSize);
    
    
  
 
  
 
    //分页的函数
     
    function news($pageNum = 1, $pageSize = 3)
     
    {
     
     $array = array();
     
     $coon = mysqli_connect("localhost", "root","");
     
     mysqli_select_db($coon, "xiangmu");
     
     mysqli_set_charset($coon, "utf8");
     
     // limit为约束显示多少条信息，后面有两个参数，第一个为从第几个开始，第二个为长度
     
     $rs = "select * from course limit " . (($pageNum - 1) * $pageSize) . "," . $pageSize;
     
     $r = mysqli_query($coon, $rs);
     
     while ($obj = mysqli_fetch_object($r)) {
     
      $array[] = $obj;
     
     }
     
   //  mysqli_close($coon,"zyq");
     
     return $array;
     
    }
     
      
     
    //显示总页数的函数
     
    function allNews()
     
    {
     
     $coon = mysqli_connect("localhost", "root","");
     
     mysqli_select_db($coon, "xiangmu");
     
     mysqli_set_charset($coon, "utf8");
     
     $rs = "select count(*) num from course"; //可以显示出总页数
     
     $r = mysqli_query($coon, $rs);
     
     $obj = mysqli_fetch_object($r);
     
    // mysqli_close($coon,"zyq");
     
     return $obj->num;
     
    }
     
      

   
    

    //定义SQL语句，查询所有学生信息
    $sql = "select * from course limit " . (($pageNum - 1) * $pageSize) . "," . $pageSize;

    //执行SQL语句，返回结果，并用表格显示信息
    if ($result = mysqli_query($db, $sql)) {
        echo "<form action ='UpdDel.php' method='post'>";
        echo "<input type='submit' name='btnDel' value='删除' onclick='return confirm(\"确定要删除选中的学生信息吗?\");'/>";
        echo "<input type='submit' name='btnUpdate' value='编辑'/>";
        echo "<input type='submit' name='btnAdd' value='添加'/>";

        echo "<table class='tb'><tr><th></th><th>课程号</th><th>课程</th></tr>";
        
        //获取数据
        while ($row = mysqli_fetch_assoc($result)) {
            echo "<tr><td><input type='checkbox' name='sel[]' value='" . $row["CourseID"] . "'/>" .
                "</td><td>" . $row["CourseID"] .
                "</td><td>" . $row["CourseName"] .

                "</td></tr>";
        }
        echo "</table>";
        echo "</form>";
        
        //释放结果集
        $result->close();
    }
    
    //关闭连接
    $db->close();
    ?>


  
  
 <div>
  
  <a href="?pageNum=1" rel="external nofollow" rel="external nofollow" >首页</a>
  
  <a href="?pageNum=<?php echo $pageNum==1?1:($pageNum-1)?>" rel="external nofollow" rel="external nofollow" >上一页</a>
  
  <a href="?pageNum=<?php echo $pageNum==$endPage?$endPage:($pageNum+1)?>" rel="external nofollow" rel="external nofollow" >下一页</a>
  
  
  
   
  
 </div>
    <?php
    include 'Footer.html';  //包含页脚页
    ?>

</body>

</html>